﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using amis6_api.Common;
using amis6_api.Model;
using Dapper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace amis6_api.Utils
{
    public class FieldsConvertUtil
    {
        public static string ColumnConvert(Guid id, string name)
        {

            using var conn = DbFactory.Db();
            var guid = conn.QueryFirst<Guid>($"select DD_ID from t_DataDictionary where DD_Code='name'");
            var value = conn.QueryFirstOrDefault<string>($"select DDL_Value from t_DDL where DD_ID='{guid}' and DDL_ID='{id}';");
            return value ?? "";
        }

        public static string ColumnConvert1(string code, string name)
        {
            using var conn = DbFactory.Db();
            var guid = conn.QueryFirst<Guid>($"select DD_ID from t_DataDictionary where DD_Code='name'");
            var value = conn.QueryFirstOrDefault<string>($"select DDL_Value from t_DDL where DD_ID='{guid}' and DDL_Code='{code}';");
            return value ?? "";
        }

        public static string UserConvert(Guid uid)
        {
            using var conn = DbFactory.Db();
            var uName = conn.QueryFirstOrDefault<string>($"select U_Name from t_Users where U_ID='{uid}'");
            return uName ?? "";
        }

        public static string CatalogConvertName(Guid cid)
        {
            using var conn = DbFactory.Db();
            var cName = conn.QueryFirstOrDefault<string>($"select Catalog_Name from t_Catalog where Catalog_ID='{cid}'");
            return cName ?? "";
        }

        public static string SmallCatalogConvertName(Guid sid)
        {
            using var conn = DbFactory.Db();
            var sName = conn.QueryFirstOrDefault<string>($"select Catalog_Name from t_SmallCatalog where Catalog_ID='{sid}'");
            return sName ?? "";
        }

        public static List<object> ConvertList(List<object> list, Guid fmtId)
        {
            using var conn = DbFactory.Db();
            var fieldConvertList =
                conn.Query<FieldRule>(
                    $"Select a.*,b.Field_Name,b.Field_CnName from t_FieldRule a left join t_Data_Fields b on a.Field_ID=b.Field_ID where a.FMT_ID='{fmtId}' and a.FR_RuleType='display';").ToList();

            string json = JsonConvert.SerializeObject(list);
            JArray jArray = (JArray)JsonConvert.DeserializeObject(json);

            foreach (var item in jArray)
            {
                foreach (var fieldRule in fieldConvertList)
                {
                    foreach (var jToken in item)
                    {
                        var jp = (JProperty)jToken;
                        var value = jp.Value;
                        string name = jp.Name;
                        if (name.ToLower()=="state")
                        {
                            jp.Value = "已归档";
                        }
                        if (fieldRule.FR_Rule.Split(':').Length==1)
                        {
                            break;
                        }
                        
                        var str = fieldRule.FR_Rule.Split(':')[0];
                        var strValue = fieldRule.FR_Rule.Split(':')[1];
                        
                        if (string.Equals(name,fieldRule.Field_Name)&& !string.IsNullOrEmpty(value.ToString()))
                        {
                            if (String.Equals(str, "UserConverter"))
                            {
                                string newValue = UserConvert(new Guid(value.ToString()));
                                jp.Value = newValue;
                            }
                            else if (String.Equals(str, "CatalogConvertName"))
                            {
                                string newValue = CatalogConvertName(new Guid(value.ToString()));
                                jp.Value = newValue;
                            }
                            else if (String.Equals(str, "ColumnConvert"))
                            {
                                string newValue = ColumnConvert(new Guid(value.ToString()), strValue);
                                jp.Value = newValue;
                            }
                            else if (String.Equals(str, "ColumnConvert1"))
                            {
                                string newValue = ColumnConvert1(value.ToString(), strValue);
                                jp.Value = newValue;
                            }
                            else if (String.Equals(str, "SmallCatalogConvertName"))
                            {
                                string newValue = SmallCatalogConvertName(new Guid(value.ToString()));
                                jp.Value = newValue;
                            }
                        }
                        
                    }

                    //var propertyInfo = item.GetType().GetProperty(fieldRule.Field_Name);
                    //if (propertyInfo != null)
                    //{
                        
                    //    var str = fieldRule.FR_Rule.Split(':')[0];
                    //    var strValue = fieldRule.FR_Rule.Split(':')[1];
                    //    var value = propertyInfo.GetValue(item);
                    //    if (String.Equals(str, "UserConverter"))
                    //    {
                    //        string newValue = UserConvert(new Guid(value.ToString()));
                    //        propertyInfo.SetValue(item, newValue);
                    //    }
                    //    else if (String.Equals(str, "CatalogConvertName"))
                    //    {
                    //        string newValue = CatalogConvertName(new Guid(value.ToString()));
                    //        propertyInfo.SetValue(item, newValue);
                    //    }
                    //    else if (String.Equals(str, "ColumnConvert"))
                    //    {
                    //        string newValue = ColumnConvert(new Guid(value.ToString()), strValue);
                    //        propertyInfo.SetValue(item, newValue);
                    //    }
                    //    else if (String.Equals(str, "ColumnConvert1"))
                    //    {
                    //        string newValue = ColumnConvert1(value.ToString(), strValue);
                    //        propertyInfo.SetValue(item, newValue);
                    //    }
                    //    else if (String.Equals(str, "SmallCatalogConvertName"))
                    //    {
                    //        string newValue = SmallCatalogConvertName(new Guid(value.ToString()));
                    //        propertyInfo.SetValue(item, newValue);
                    //    }
                    //}
                }
            }

            return jArray.ToObject<List<object>>();
        }
    }
}
